package com.makingTeam.comment.List;

import org.springframework.util.CollectionUtils;

import java.util.List;
import java.util.concurrent.CopyOnWriteArrayList;

/**
 * @author PC
 * @Description
 * @create 2022-10-11:26
 */
public class ListUtils {

    /**
     * 拆分
     * @param paramList 源数据
     * @param poolSize  多少个数一组
     * @param <T>
     * @return
     */
    public static  <T>List<List<T>>  partition(List<T> paramList, int poolSize) throws Exception {
        List<List<T>> list=new CopyOnWriteArrayList<>();
        if(CollectionUtils.isEmpty(paramList)){
            throw new Exception("源数据为空");
        }
        int size=paramList.size();
        if(poolSize>=size){
            list.add(paramList);
            return list;
        }
        //拆分多少个集合
        int forSize=size%poolSize==0?size/poolSize:(size/poolSize)+1;
        int startIndex=0;
        int endIndex=poolSize;
        for (int i = 0; i < forSize; i++) {
            list.add(paramList.subList(startIndex,endIndex));
            startIndex=endIndex;
            endIndex=endIndex+poolSize;
            if(endIndex>size){
                endIndex=size;
            }
        }
        return list;
    }
}
